package org.wisigoth.chat.swing.accountframe;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import org.wisigoth.chat.client.Presence;

import com.sun.org.apache.bcel.internal.classfile.JavaClass;

public class PresenceCellListRenderer extends JPanel implements ListCellRenderer {
	
	private JLabel presenceNameLabel;
	private JLabel presenceIconLabel;
	
	public PresenceCellListRenderer(){
		initComponents();
	}
	

	private void initComponents() {
		this.presenceNameLabel = new JLabel();
		this.presenceIconLabel = new JLabel();
		
        this.setLayout(new GridBagLayout());
		
		GridBagConstraints constraintsNamePresence = new GridBagConstraints();
		constraintsNamePresence.gridx = 0;
		constraintsNamePresence.gridy = 0;
		constraintsNamePresence.fill  = GridBagConstraints.HORIZONTAL;
		constraintsNamePresence.weightx = 1.0;
		constraintsNamePresence.anchor = java.awt.GridBagConstraints.CENTER;
		this.add(this.presenceNameLabel, constraintsNamePresence);
		
		GridBagConstraints constraintsIconPresence = new GridBagConstraints();
		constraintsIconPresence.gridx = 1;
		constraintsIconPresence.gridy = 0;
		constraintsIconPresence.fill  = GridBagConstraints.HORIZONTAL;
		constraintsIconPresence.anchor = java.awt.GridBagConstraints.EAST;
		this.add(this.presenceIconLabel, constraintsIconPresence);
		
	}


	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		Presence presence = (Presence)value;
		if(presence == null) {
			this.presenceNameLabel.setText(""); 
			this.presenceIconLabel.setIcon(null);
		}else {
			this.presenceNameLabel.setText(presence.getPresenceName()); 
			this.presenceIconLabel.setIcon(presence.getPresenceImage());	
		}
		if(isSelected){
			this.setBackground(Color.LIGHT_GRAY);
		}else{
			this.setBackground(Color.WHITE);
		}
		
		return this;
	}

}
